﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BTS6000
{
    public partial class frmSQL_LoadParameter : Form
    {
        #region 窗体

        public frmSQL_LoadParameter()
        {
            InitializeComponent();
        }

        private void frmSQL_LoadParameter_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.Tools;

            Language.ToCHT(this);

            Kuge.AppSetting.INI.Get("LoadParameter", this.Controls);
        }

        private void frmSQL_LoadParameter_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;

            txtOutput.Clear();

            Kuge.AppSetting.INI.Set("LoadParameter", this.Controls);

            e.Cancel = false;
        } 

        #endregion

        #region 快捷功能

        private void lnkToday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt);
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now.AddDays(-1);

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt);
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkTheDayBeforeYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now.AddDays(-2);

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt);
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkFromYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt.AddDays(-1));
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        private void lnkFromTheDayBeforeYesterday_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime dt = DateTime.Now;

            txtFrom.Text = string.Format("{0:yyyy-MM-dd} 00:00:00", dt.AddDays(-2));
            txtTo.Text = string.Format("{0:yyyy-MM-dd} 23:59:59", dt);
        }

        #endregion

        #region 参数下载

        private void btnSearch_Click(object sender, EventArgs e)
        {
            #region Search

            DateTime f, t;
            if (!DateTime.TryParse(txtFrom.Text, out f))
            {
                MessageBox.Show("Datetime is invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFrom.Focus();
                txtFrom.SelectAll();
                return;
            }
            if (!DateTime.TryParse(txtTo.Text, out t))
            {
                MessageBox.Show("Datetime is invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTo.Focus();
                txtTo.SelectAll();
                return;
            }

            if (BTS6000.SQL.Instance == null)
            {
                MessageBox.Show("No SQL Connection!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string columns, conditions;
            DataTable table;

            this.Cursor = Cursors.WaitCursor;

            lstOutput.Items.Clear();

            columns = "[ID],[Model],[Datetime],[Note]";
            conditions = string.Format("[Datetime] BETWEEN '{0}' AND '{1}'", txtFrom.Text, txtTo.Text);

            table = BTS6000.SQL.Instance.Select("[TP_6000_Plus_Parameter]", columns, conditions);

            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    ListViewItem item = new ListViewItem();

                    item.Text = string.Format("{0}", row[0]);

                    for (int i = 1; i < table.Columns.Count; i++)
                    {
                        ListViewItem.ListViewSubItem subItem = new ListViewItem.ListViewSubItem();
                        subItem.Text = string.Format("{0}", row[i]);

                        item.SubItems.Add(subItem);
                    }

                    lstOutput.Items.Add(item);
                }
            }

            this.Cursor = Cursors.Default;

            #endregion
        }

        private void lstOutput_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtOutput.Clear();

            if ((lstOutput.SelectedItems != null) && (lstOutput.SelectedItems.Count>0))
            {
                int ID;
                ListViewItem itm;

                itm = lstOutput.SelectedItems[0];

                if (int.TryParse(itm.Text, out ID))
                {
                    #region Search

                    string columns, conditions;
                    DataTable table;

                    this.Cursor = Cursors.WaitCursor;

                    columns = "[Parameter]";
                    conditions = string.Format("[ID]={0}", ID);

                    table = BTS6000.SQL.Instance.Select("[TP_6000_Plus_Parameter]", columns, conditions);

                    if (table != null)
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            txtOutput.Text = (string)row[0];
                            break;
                        }
                    }

                    this.Cursor = Cursors.Default;

                    #endregion
                }
            }
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (Testpad.BTS.Editor.Instance == null)
            {
                MessageBox.Show(Program.MDI.SR.DeviceIsNotReady, Program.MDI.SR.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if(string.IsNullOrEmpty(txtOutput.Text))
                {
                    MessageBox.Show(Program.MDI.SR.DataCanNotBeBlank, Program.MDI.SR.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Testpad.BTS.Editor.Instance.FromXML(txtOutput.Text);
                    Testpad.BTS.BTS6000.Instance.Parameter_Download(Testpad.BTS.Editor.Instance.Parameter);
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtOutput.Text))
            {
                MessageBox.Show(Program.MDI.SR.DataCanNotBeBlank, Program.MDI.SR.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "*.xml|*.xml";
                if(dlg.ShowDialog() == DialogResult.Yes)
                {
                    using (StreamWriter sw = new StreamWriter(dlg.FileName, false, Encoding.UTF8))
                    {
                        sw.Write(txtOutput.Text);
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
        } 

        #endregion
    }
}
